from appium.webdriver.webdriver import WebDriver
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class BasePage:
    def __init__(self, driver: WebDriver = None):
        self.driver = driver

    def find(self, by, value, timeout=10):
        try:
            WebDriverWait(self.driver, timeout).until(expected_conditions.visibility_of_element_located((by, value)))
        except TimeoutException:
            raise NoSuchElementException(f"{(by, value)}元素未找到")
        except Exception as e:
            raise RuntimeError(f"{(by, value)}元素定位失败，{e}")
        return self.driver.find_element(by, value)

    def finds(self, by, value, timeout=10):
        try:
            WebDriverWait(self.driver, timeout).until(expected_conditions.visibility_of_element_located((by, value)))
        except TimeoutException:
            return  []
        return self.driver.find_elements(by, value)

    def get_screen_size(self):
        return self.driver.get_window_size()

    def swipe_down(self, t=500):
        screen_size = self.get_screen_size()
        x1 = screen_size['width']*0.5
        y1 = screen_size['height']*0.25
        y2 = screen_size['height']*0.75
        self.driver.swipe(x1,y1,x1,y2,t)





